<?php
require_once('TVB/pms/pdes/action/InfoGetAction.class.php');

class InfoBiographyUpdateAction extends BaseAction 
{
	public $id;
	public $episode_id;
	public $name_chinese;
	public $name_english;
	public $nickname;
	public $gender;
	public $date_of_birth_year;
	public $date_of_birth_month;
	public $date_of_birth_day;
	#public $origin_country;
	public $country;
	#public $origin_province_state_prefecture;
	public $province_state;
	public $origin_city;
	public $occupation;
	public $description;
	
	/**
	 * @filter datetime
	 * @label Publish Date/Time
	 */
    public $publish_datetime;
	
	/**
	 * @filter datetime
	 * @label Expire Date/Time
	 */
    public $expire_datetime;
	
	public $status;
	public $created_by;
	public $created_at;
	public $modified_by;
	public $modified_at;
	
	public $programme_id;
	public $onair_episode_no;

	public $ut;
	public $reset;
	public $from;
	public function execute(){
		if (!is_null($this->reset))
		{
			$this->setData('id', $this->id);
			$this->setData('type', 'biography');
			$this->setData('act', 'edit');
			$this->setData('from', $this->from);
			return 'reset';
		}

		Model::$dbo['w']->beginTransaction();
		try
		{
			if (CommonUtils::is_concurrent('info_biography', $this->id, $this->modified_at))
			{
				$this->init_page_data();
				$this->addError('concurrent', array('0105', 'Concurrent update found!'));
				return 'logic-error';
			}

			if(count($this->_errors) > 0){
				$this->init_page_data();
				return 'filter-error';
			}

			if(!CommonUtils::check_date($this->date_of_birth_year, $this->date_of_birth_month, $this->date_of_birth_day))
			{
				$this->init_page_data();
				$this->addError('Biography.Date of Birth', array('0005', 'Date of Birth'));
				return 'filter-error';
			}

			//check name unique
			if (is_null($this->name_chinese) && is_null($this->name_english))
			{
				$this->init_page_data();
				$this->addError('Biography.Name', array('0044', 'Name'));
				return 'filter-error';
			}

            //check publish_datetime && expire_datetime
            if (empty($this->publish_datetime)
                || empty($this->expire_datetime)
                || $this->publish_datetime > $this->expire_datetime) {

                $this->init_page_data();
                $this->addError('Publish Date/Time && Expire Date/Time', array('0033', 'Publish Date/Time && Expire Date/Time'));
                return 'filter-error';
            }

			$update_id = $this->update_info_biography();
			if ($update_id)
			{
				$this->setData('type','biography');
				$this->setData('onair_episode_no', $this->onair_episode_no);
				$this->setData('programme_id', $this->programme_id);
			}

			Model::$dbo['w']->commit();

			return ($this->from == 'task') ? "task" : "succ";
		}
		catch(Exception $e)
		{
			error_log("Update biography Fails: {$this->id}" . var_export($this, 1));
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}    

	/**
	 * update info_biography
	 */
	private function update_info_biography()
	{
		$info_biography = new info_biography();
		$info_biography = $info_biography->get($this->id);
		Model::diff($info_biography, $this);
		$info_biography->modified_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$info_biography->modified_by = CommonUtils::get_user();
		$info_biography->origin_province_state_prefecture = $this->province_state;
		$info_biography->origin_country = $this->country;
		$info_biography->name_chinese = trim($this->name_chinese);
		$info_biography->name_english = trim($this->name_english);
		$info_biography->nickname = trim($this->nickname);
		$info_biography->origin_city = trim($this->origin_city);
		$info_biography->occupation  = trim($this->occupation);
        $info_biography->publish_datetime = $this->publish_datetime;
        $info_biography->expire_datetime = $this->expire_datetime;

		$update_id = Model::$dbo['w']->save($info_biography);
		if ($update_id > 0)
		{
            $primary_key = array(
                'id'    => $this->id
            );
            $fields = array(
                'name_chinese',
                'name_english',
                'nickname',
                'origin_city',
                'occupation',
                'description'
            );
            CommonUtils::filterC2A0('info_biography', $primary_key, $fields);

			$info_biography->add_change_log();

			$this->addInfo("info_biography:".AC::LOG_OP_UPDATE.":".$this->id, "update info_biography success", json_encode($info_biography));
			return true;
		}
		else
		{
			$this->addError("info_biography:".AC::LOG_OP_UPDATE.":".$this->id, "update info_biography failed", json_encode($info_biography));
			return false;
		}
	}

	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$action = new InfoGetAction();
		$action->id = $this->id;
		$action->act = 'edit';
		$action->type = 'biography';
		$action->execute();

		$old_data = $action->getData();
		$this->modified_at = $old_data['biography']->modified_at;
		$old_data['biography'] = Model::diff($old_data['biography'], $this);
		$old_data['biography']->country_name  = country::getCountryName($this->country);
		$old_data['biography']->origin_country = !empty($this->country) ? $this->country : '';
		$old_data['biography']->province_name  = province_state_prefecture::getProvinceName($this->province_state);
		$old_data['biography']->origin_province_state_prefecture = !empty($this->province_state) ? $this->province_state : '';
		foreach ($old_data as $object=>$data)
		{
			$this->setData($object, $data);
		}

		//jump page
		$this->setData('id', $this->id);
		$this->setData('type', 'biography');
		$this->setData('act', 'edit');
		$this->setData('from', $this->from);
	}
}
?>
